package org.pyq.agent.bytekit;

import com.alibaba.bytekit.asm.binding.Binding;
import com.alibaba.bytekit.asm.interceptor.annotation.*;
import org.springframework.data.redis.core.RedisTemplate;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * @author pengYongQiang
 * @date 2021/12/20 17:41
 */
public class SampleInterceptor {
    @AtEnter(inline = true, suppress = RuntimeException.class)
    @AtLine(lines = { -1}, inline = true)
    public static void atEnter(@Binding.This Object object,
                               @Binding.Class Object clazz,
                               @Binding.Args Object[] args,
                               @Binding.MethodName String methodName,
                               @Binding.MethodDesc String methodDesc,
                               @Binding.LocalVars Object[] localVars) {

        System.out.println("atEnter, object: " + object);
        System.out.println("atEnter, clazz: " + clazz);

        System.out.println("atEnter, args[0]: " + args[0]);
        System.out.println("atEnter, methodName " + methodName);
        System.out.println("atEnter, methodDesc: " + methodDesc);
        System.out.println("atEnter, localVars: " + Arrays.toString(localVars));
        if (localVars.length==3){
            System.out.println(localVars);
            System.out.println("变量:"+localVars[2]);
            System.out.println(localVars[2]);
            localVars[2] = "bbbbb";
        }

                               }
//    @AtEnter(inline = true, suppress = RuntimeException.class)
//    public static void atEnter(@Binding.This Object object,
//                               @Binding.Class Object clazz,
//                               @Binding.Args Object[] args,
//                               @Binding.MethodName String methodName,
//                               @Binding.MethodDesc String methodDesc,
//                               @Binding.LocalVars Object[] localVars,
//                               @Binding.Field(name = "redisTemplate") Object redisTemplate) {
//        System.out.println("atEnter, object: " + object);
//        System.out.println("atEnter, clazz: " + clazz);
//
//        System.out.println("atEnter, args[0]: " + args[0]);
//        System.out.println("atEnter, methodName " + methodName);
//        System.out.println("atEnter, methodDesc: " + methodDesc);
//        System.out.println("atEnter, localVars: " + Arrays.toString(localVars));
//
//        HttpServletRequest request = (HttpServletRequest) args[0];
//        String token = request.getHeader("token");
//        String token1 = request.getParameter("token");
//        if (token != null || token1 != null) {
//            return;
//        }
//        RedisTemplate<String, String> stringStringRedisTemplate = (RedisTemplate<String, String>) redisTemplate;
//
//        String account = request.getHeader("account");
//        if (account == null || "".equals(account.trim())) {
//            return;
//        }
//        if (account.startsWith("t-")) {
//            String uuid = account.split("t-")[1];
//            //token_Tenant_268_n
//            String tokenKey = "token_Tenant_" + uuid + "_n";
//            token = (String) stringStringRedisTemplate.opsForHash().get(tokenKey, "token");
//        }
//        if (account.startsWith("m-")) {
//            String uuid = account.split("m-")[1];
//            String tokenKey = "token_Manager_" + uuid + "_n";
//            token = (String) stringStringRedisTemplate.opsForHash().get(tokenKey, "token");
//        }
//
//        System.out.println("获取到token:" + token);
//    }




//    @AtExit(inline = true)
//    public static void atExit(@Binding.Return Object returnObject) {
//        System.out.println("atExit, returnObject: " + returnObject);
//    }
//
//    @AtExceptionExit(inline = true, onException = RuntimeException.class)
//    public static void atExceptionExit(@Binding.Throwable RuntimeException ex) {
//        System.out.println("atExceptionExit, ex: " + ex.getMessage());
//    }
}
